package GUI;

import java.awt.EventQueue;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.sql.Date;
import java.util.Currency;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JSpinner;
import javax.swing.JInternalFrame;
import javax.swing.JSplitPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.omg.CORBA.Current;

import Control.Ctr;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListSelectionModel;
import javax.swing.Box;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class Admin {

	//private static final  Date a = new Date();
	private JFrame frame;
	private JTextField textcoLoc;
	private JTable table_3;
	private JTextField textchSSN;
	private JTextField textchname;
	private JTextField textchphone;
	private JTextField textchaddress;
	private JTextField txtInputchID;
	private JTable table_5;
	private JTextField txtSingleUpdate_1;
	private JTable table;
	private JTextField txtFindCoID;
	private JTextField txtDate;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin window = new Admin();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Admin() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		final Ctr ctr = new Ctr();
		frame = new JFrame();
		frame.setBounds(100, 100, 546, 482);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frame.getContentPane().add(tabbedPane);
		
		JPanel Coach = new JPanel();
		tabbedPane.addTab("Coach", null, Coach, null);
		
		JLabel lblName_1 = new JLabel("Name");
		lblName_1.setBounds(10, 36, 56, 14);
		
		JLabel lblAddress_1 = new JLabel("Address");
		lblAddress_1.setBounds(208, 36, 71, 14);
		
		JLabel lblPhoneNumber_1 = new JLabel("Phone Number");
		lblPhoneNumber_1.setBounds(208, 11, 92, 14);
		
		JLabel lblSsn_1 = new JLabel("SSN");
		lblSsn_1.setBounds(10, 11, 56, 14);
		
		textchSSN = new JTextField();
		textchSSN.setBounds(76, 8, 122, 20);
		textchSSN.setColumns(10);
		
		textchname = new JTextField();
		textchname.setBounds(76, 33, 122, 20);
		textchname.setColumns(10);
		
		textchphone = new JTextField();
		textchphone.setBounds(301, 8, 118, 20);
		textchphone.setColumns(10);
		
		textchaddress = new JTextField();
		textchaddress.setBounds(301, 33, 118, 20);
		textchaddress.setColumns(10);
		
		JButton btnAddTrainer = new JButton("Add Coach");
		btnAddTrainer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = textchname.getText();
				String address = textchaddress.getText();
				String SSN = textchSSN.getText();
				int phone = Integer.parseInt(textchphone.getText());
				ctr.insertNewCoach(name, address, SSN, phone);
			}
		});
		btnAddTrainer.setBounds(429, 7, 110, 23);
		Coach.setLayout(null);
		Coach.add(lblSsn_1);
		Coach.add(textchSSN);
		Coach.add(lblName_1);
		Coach.add(textchname);
		Coach.add(lblPhoneNumber_1);
		Coach.add(lblAddress_1);
		Coach.add(textchaddress);
		Coach.add(textchphone);
		Coach.add(btnAddTrainer);
		
		txtInputchID = new JTextField();
		txtInputchID.setText("Input chID");
		txtInputchID.setBounds(10, 76, 110, 20);
		Coach.add(txtInputchID);
		txtInputchID.setColumns(10);
		
		JButton btnFindTrainer = new JButton("Find Coach");
		btnFindTrainer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Model.Coach c = ctr.findBychID(Integer.parseInt(txtInputchID.getText()));
				TableModel model = new CoachTable(c);
				table_5.setModel(model);
			}
		});
		btnFindTrainer.setBounds(124, 75, 110, 23);
		Coach.add(btnFindTrainer);
		
		JButton btnListAll_2 = new JButton("List ALL");
		btnListAll_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TableModel model = new CoachTable();
				table_5.setModel(model);
			}
		});
		btnListAll_2.setBounds(225, 351, 110, 23);
		Coach.add(btnListAll_2);
		
		JScrollPane scrollPane_5 = new JScrollPane();
		scrollPane_5.setBounds(10, 141, 518, 198);
		Coach.add(scrollPane_5);
		
		table_5 = new JTable();
		table_5.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
},
			new String[] {
				"ID", "SSN", "Name", "Address", "Phone Number"
			}
		));
		table_5.getColumnModel().getColumn(0).setPreferredWidth(48);
		table_5.getColumnModel().getColumn(1).setPreferredWidth(94);
		table_5.getColumnModel().getColumn(2).setPreferredWidth(93);
		table_5.getColumnModel().getColumn(3).setPreferredWidth(105);
		table_5.getColumnModel().getColumn(4).setPreferredWidth(109);
		scrollPane_5.setViewportView(table_5);
		
		JButton btnIllBeBach_1 = new JButton("Close");
		btnIllBeBach_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				
			}
		});
		btnIllBeBach_1.setBounds(212, 396, 138, 23);
		Coach.add(btnIllBeBach_1);
		
		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "SSN", "Name", "Address", "Phone Number"}));
		comboBox_1.setBounds(271, 76, 122, 20);
		Coach.add(comboBox_1);
		
		JButton btnUpdateSelected_1 = new JButton("Update");
		btnUpdateSelected_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = table_5.getSelectedRow();
				int y = 0;
				int chID = (int) table_5.getValueAt(x,y);
				
				switch(comboBox_1.getSelectedIndex())
				{case 0: //SSN
					ctr.updateCoach(chID,2,txtSingleUpdate_1.getText(),-1);
					break;
				case 1: //Name
					ctr.updateCoach(chID, 0, txtSingleUpdate_1.getText(), -1);
					break;
				case 2: //Address
					ctr.updateCoach(chID, 1, txtSingleUpdate_1.getText(), -1);
					break;
				case 3: //Phone
					ctr.updateCoach(chID, 0, "",Integer.parseInt(txtSingleUpdate_1.getText()) );
					break;

				}
			}
		});
		btnUpdateSelected_1.setBounds(403, 75, 89, 23);
		Coach.add(btnUpdateSelected_1);
		
		JButton btnDeleteSelected_1 = new JButton("Delete Selected");
		btnDeleteSelected_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x =table_5.getSelectedRow();
				int y =0;
				int del =(int) table_5.getValueAt(x, y);
				ctr.deleteCoach(del);
				
			}
		});
		btnDeleteSelected_1.setBounds(10, 107, 110, 23);
		Coach.add(btnDeleteSelected_1);
		
		
		
		txtSingleUpdate_1 = new JTextField();
		txtSingleUpdate_1.setText("Single Update");
		txtSingleUpdate_1.setBounds(271, 107, 122, 20);
		Coach.add(txtSingleUpdate_1);
		txtSingleUpdate_1.setColumns(10);
		
		JPanel Invoice = new JPanel();
		tabbedPane.addTab("Invoice", null, Invoice, null);
		Invoice.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 518, 145);
		Invoice.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
			},
			new String[] {
				"ID", "Booking Nr.", "Client Name", "Coach", "Price", "Sessions Bought"
			}
		));
		table.getColumnModel().getColumn(5).setPreferredWidth(98);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setColumnSelectionAllowed(true);
		table.setCellSelectionEnabled(true);
		scrollPane.setViewportView(table);
		
		JButton btnListAllInvoices = new JButton("List all Invoices");
		btnListAllInvoices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel model = new InvoiceTable();
				table.setModel(model);
			}
		});
		btnListAllInvoices.setBounds(307, 167, 137, 23);
		Invoice.add(btnListAllInvoices);
		
		JButton btnListByDate = new JButton("List All in Date");
		btnListByDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TableModel model = new InvoiceTable(txtDate.getText());
				table.setModel(model);
			}
		});
		btnListByDate.setBounds(162, 167, 135, 23);
		Invoice.add(btnListByDate);
		
		txtDate = new JTextField();
		txtDate.setText("Date");
		txtDate.setBounds(20, 167, 132, 20);
		Invoice.add(txtDate);
		txtDate.setColumns(10);
		
		JPanel Court = new JPanel();
		tabbedPane.addTab("Court", null, Court, null);
		
		JLabel lblLocation = new JLabel("Location");
		lblLocation.setBounds(51, 15, 70, 14);
		
		textcoLoc = new JTextField();
		textcoLoc.setBounds(145, 12, 102, 20);
		textcoLoc.setColumns(10);
		
		JButton btnAddCourt = new JButton("Add Court");
		btnAddCourt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ctr.insertNewCourt(textcoLoc.getText());
			}
		});
		btnAddCourt.setBounds(257, 11, 158, 23);
		
		JButton btnFindCourt = new JButton("Find Court");
		btnFindCourt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Model.Court co = ctr.findBycoID(Integer.parseInt(txtFindCoID.getText()));
				TableModel model = new CourtTable(co);
				table_3.setModel(model);
			}
		});
		btnFindCourt.setBounds(257, 45, 158, 23);
		
		JButton btnListAll_1 = new JButton("List ALL");
		btnListAll_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel model = new CourtTable();
				table_3.setModel(model);
			}
		});
		btnListAll_1.setBounds(89, 79, 158, 23);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(10, 113, 513, 306);
		
		table_3 = new JTable();
		table_3.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
			},
			new String[] {
				"ID", "Location"
			}
		));
		table_3.getColumnModel().getColumn(0).setPreferredWidth(44);
		scrollPane_3.setViewportView(table_3);
		Court.setLayout(null);
		Court.add(lblLocation);
		Court.add(textcoLoc);
		Court.add(btnAddCourt);
		Court.add(btnFindCourt);
		Court.add(btnListAll_1);
		Court.add(scrollPane_3);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.setBounds(721, 74, 89, 23);
		Court.add(btnUpdate);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.setBounds(721, 114, 89, 23);
		Court.add(btnDelete);
		
		JButton btnDeleteCo = new JButton("Delete Selected");
		btnDeleteCo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			int x = table_3.getSelectedRow();
			int y = 0;
			int coID = (int) table_3.getValueAt(x,y);
			ctr.deleteCourt(coID);
			}
		});
		btnDeleteCo.setBounds(257, 79, 158, 23);
		Court.add(btnDeleteCo);
		
		txtFindCoID = new JTextField();
		txtFindCoID.setBounds(145, 46, 102, 20);
		Court.add(txtFindCoID);
		txtFindCoID.setColumns(10);
		
		JLabel lblFindCourt = new JLabel("Find Court");
		lblFindCourt.setBounds(51, 49, 70, 14);
		Court.add(lblFindCourt);
	}
}
